import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JSlider;

import org.jfree.chart.ChartPanel;


public class SimulationUI extends JFrame {

	private JPanel contentPane;
	private JTextField txtLower;
	private JTextField txtPopulationSize;
	private JTextField txtSampleSize;
	private JTable tblPopulation;
	private JPanel rangePanel;
	private JPanel populationPanel;
	private JTextField txtUpper;
	private JPanel sampleSizePanel;
	private JPanel distributionPanel;
	private JPanel resultPanel;
	private JComboBox cmbDistribution;
	private JButton btnGenerate;
	private JLabel lblPopulationMean;
	private JLabel lblPopulationVariance;
	private JLabel lblSampleMean;
	private JLabel lblSampleVariance;
	private JSlider sldrPopulation;
	private JTable tblSample;
	private JPanel SampleSliderPanel;
	private JSlider sldrSample;
	private JPanel panel_1;
	private JPanel populationGraphPanel;
	private JPanel sampleGraphPanel;
	private JLabel lbln;
	private JLabel lblN;
	private JScrollPane populationScrollPanel;
	private JScrollPane sampleScrollPanel;


	/**
	 * Create the frame.
	 */
	public SimulationUI(){
		initGUI();
	}
	public void initGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1026, 695);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[246.00][201.00][grow]", "[341.00,grow][305.00,grow]"));
		
		JPanel panel = new JPanel();
		contentPane.add(panel, "cell 0 0 1 2,grow");
		panel.setLayout(new MigLayout("", "[295.00,grow,left]", "[][][][][grow]"));
		
		rangePanel = new JPanel();
		panel.add(rangePanel, "cell 0 0,alignx left,growy");
		
		JLabel lblRange = new JLabel("Range:");
		rangePanel.add(lblRange);
		
		txtLower = new JTextField();
		rangePanel.add(txtLower);
		txtLower.setColumns(10);
		
		JLabel label = new JLabel("-");
		rangePanel.add(label);
		
		txtUpper = new JTextField();
		rangePanel.add(txtUpper);
		txtUpper.setColumns(10);
		
		populationPanel = new JPanel();
		panel.add(populationPanel, "cell 0 1,alignx left,growy");
		
		JLabel lblPopulationSize = new JLabel("Population Size:");
		populationPanel.add(lblPopulationSize);
		
		txtPopulationSize = new JTextField();
		populationPanel.add(txtPopulationSize);
		txtPopulationSize.setColumns(10);
		
		sampleSizePanel = new JPanel();
		panel.add(sampleSizePanel, "cell 0 2,alignx left,growy");
		
		JLabel lblNewLabel = new JLabel("Sample Size:");
		sampleSizePanel.add(lblNewLabel);
		
		txtSampleSize = new JTextField();
		sampleSizePanel.add(txtSampleSize);
		txtSampleSize.setColumns(10);
		
		distributionPanel = new JPanel();
		panel.add(distributionPanel, "flowy,cell 0 3,alignx left,growy");
		distributionPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblDistribution = new JLabel("Distribution:");
		distributionPanel.add(lblDistribution);
		
		cmbDistribution = new JComboBox();
		cmbDistribution.setEditable(true);
		cmbDistribution.setModel(new DefaultComboBoxModel(new String[] {"Uniform", "Skewed", "Normal", "Bimodal", "Random"}));
		distributionPanel.add(cmbDistribution);
		
		btnGenerate = new JButton("Generate");
		distributionPanel.add(btnGenerate);
		
		resultPanel = new JPanel();
		panel.add(resultPanel, "cell 0 4,grow");
		resultPanel.setLayout(new MigLayout("", "[]", "[][][][][][][][][][][][][][][][][]"));
		
		JLabel lblPopulation = new JLabel("Population");
		resultPanel.add(lblPopulation, "cell 0 0");
		
		lblPopulationMean = new JLabel("Mean = ");
		resultPanel.add(lblPopulationMean, "cell 0 1,alignx left");
		
		lblPopulationVariance = new JLabel("Variance =");
		resultPanel.add(lblPopulationVariance, "cell 0 2");
		
		JLabel lblSample = new JLabel("Sample");
		resultPanel.add(lblSample, "cell 0 4");
		
		lblSampleMean = new JLabel("Mean = ");
		resultPanel.add(lblSampleMean, "cell 0 5");
		
		lblSampleVariance = new JLabel("Variance = ");
		resultPanel.add(lblSampleVariance, "cell 0 6");
		
		panel_1 = new JPanel();
		resultPanel.add(panel_1, "cell 0 15");
		panel_1.setLayout(new MigLayout("", "[235px,grow]", "[]"));
		
		lblN = new JLabel("N = 0");
		panel_1.add(lblN, "flowx,cell 0 0");
		
		sldrPopulation = new JSlider();
		panel_1.add(sldrPopulation, "cell 0 0");
		
		SampleSliderPanel = new JPanel();
		resultPanel.add(SampleSliderPanel, "cell 0 16");
		SampleSliderPanel.setLayout(new MigLayout("", "[][]", "[]"));
		
		lbln = new JLabel("n = 0 ");
		SampleSliderPanel.add(lbln, "cell 0 0");
		
		sldrSample = new JSlider();
		SampleSliderPanel.add(sldrSample, "cell 1 0");
		
		JPanel PopulationPanel = new JPanel();
		contentPane.add(PopulationPanel, "cell 1 0,grow");
		PopulationPanel.setLayout(new MigLayout("", "[235px,grow]", "[224.00px,grow][]"));
		
		JScrollPane scrollPane = new JScrollPane();
		PopulationPanel.add(scrollPane, "cell 0 0,grow");
		
		tblPopulation = new JTable();
		scrollPane.setViewportView(tblPopulation);
		
		populationGraphPanel = new JPanel();
		contentPane.add(populationGraphPanel, "cell 2 0,grow");
		populationGraphPanel.setLayout(new MigLayout("", "[grow]", "[grow]"));
		
		populationScrollPanel = new JScrollPane();
		populationGraphPanel.add(populationScrollPanel, "cell 0 0,grow");
		
		JPanel SamplePanel = new JPanel();
		contentPane.add(SamplePanel, "cell 1 1,grow");
		SamplePanel.setLayout(new MigLayout("", "[grow]", "[228.00,grow][grow]"));
		
		JScrollPane scrollPane_1 = new JScrollPane();
		SamplePanel.add(scrollPane_1, "cell 0 0,grow");
		
		tblSample = new JTable();
		scrollPane_1.setViewportView(tblSample);
		
		sampleGraphPanel = new JPanel();
		contentPane.add(sampleGraphPanel, "cell 2 1,grow");
		sampleGraphPanel.setLayout(new MigLayout("", "[grow]", "[grow]"));
		
		sampleScrollPanel = new JScrollPane();
		sampleGraphPanel.add(sampleScrollPanel, "cell 0 0,grow");
	}
	
	// This is for the user's input
	public void setButtonListener(ActionListener al){
		btnGenerate.addActionListener(al);
	}
	
	public int getTxtLower(){
		return Integer.parseInt(txtLower.getText());
	}
	
	public int getTxtUpper(){
		return Integer.parseInt(txtUpper.getText());
	}
	
	public int getTxtPopulationSize(){
		return Integer.parseInt(txtPopulationSize.getText());
	}
	
	public int getTxtSampleSize(){
		return Integer.parseInt(txtSampleSize.getText());
	}
	
	public String getDistribution(){
		return cmbDistribution.getSelectedItem().toString();
	}
	
	// Population
	
	public void setPopulationMean(double value){
		lblPopulationMean.setText("Mean = " + Double.toString(value));
	}
	
	public void setPopulationVariance(double value){
		lblPopulationVariance.setText("Variance = " + Double.toString(value));
	}
	
	public void setPopulationTableModel(DefaultTableModel model){
		tblPopulation.setModel(model);
	}
	
	public void setPopulationSliderValues(int lower, int upper){
		sldrPopulation.setMinimum(lower);
		sldrPopulation.setMaximum(upper);
	}
	
	public void setCurrentPopulationValue(int num){
		lblN.setText("N = " + num);
	}
	
	public void setCurrentSampleValue(int num){
		lbln.setText("n = " + num);
	}
	
	public int getPopulationSliderValue(){
		return sldrPopulation.getValue();
	}
	public void setPopulationSliderListener(ChangeListener cl){
		 sldrPopulation.addChangeListener(cl);
	}
	
	public void setPopulationChart(ChartPanel chart){
		populationScrollPanel.setViewportView(chart);
		//populationGraphPanel.add(pane);
	}
	
	// Sample
	
	public void setSampleTableModel(DefaultTableModel model){
		tblSample.setModel(model);
	}
	
	public void setSampleChart(ChartPanel chart){
		sampleScrollPanel.setViewportView(chart);
		//populationGraphPanel.add(pane);
	}
}
